package com.sy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sy.entity.Admin;
import com.sy.service.AdminService;
import com.sy.utils.RandomUtils;
import com.sy.utils.RedisOperations;
import com.sy.utils.SMSUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static com.sy.constant.BusinessConstant.*;
import static com.sy.constant.RegConstant.PHONE_REGEX;

@Service
public class AdminServiceImpl extends BaseServiceImpl implements AdminService {
    @Autowired
    private RedisOperations redisOperations;

    @Override
    public void sendValidateCode(String phone) {
        if (StringUtils.isBlank(phone)) {
            buildError(HttpStatus.NOT_FOUND,"手机号不能为空");
        }
        if (!phone.matches(PHONE_REGEX)) {
            buildError(HttpStatus.BAD_REQUEST, "手机号格式不合法");
        }

        Admin adminByPhone = adminMapper.getAdminByPhone(phone);
        if (Objects.isNull(adminByPhone)) {
            buildError(HttpStatus.NOT_FOUND,"该手机号不为管理员！");
        }

        //要发送的验证码
        String validateCode = RandomUtils.getRandomString(4);

        //调用短信平台发送短信验证码
        JSONObject resultJson = SMSUtils.sendValidateCode(phone, validateCode);
        if (resultJson.getIntValue("result") != SMS_SUCCESS_CODE) {
            buildError(HttpStatus.BAD_REQUEST, "短信发送失败，请重试！");
        }
        //如果短信发送成功，则在Redis中保存当前手机号收到的验证码，用于后面注册逻辑中的比对操作
        else {
            String key = REDIS_VALIDATE_CODE_KEY_PREFIX + phone;
            redisOperations.set(key, validateCode);
            redisOperations.expire(key, VALIDATE_CODE_EXPIRE_SECONDS);
        }
    }

    @Override
    public Admin loginByName(String adminName, String pwd) {
        if (StringUtils.isBlank(adminName)) {
            buildError(HttpStatus.NOT_FOUND,"用户名不能为空");
        }
        if (StringUtils.isBlank(pwd)) {
            buildError(HttpStatus.NOT_FOUND,"密码不能为空");
        }

        Admin adminByName = adminMapper.getAdminByName(adminName);
        if (Objects.isNull(adminByName)) {
            buildError(HttpStatus.NOT_FOUND,"用户不存在");
        }
        if (!StringUtils.equals(adminByName.getPwd(), pwd)) {
            buildError(HttpStatus.BAD_REQUEST,"密码错误");
        }

        adminByName.setPwd(null);

        return adminByName;
    }

    @Override
    public Admin loginByPhone(String phone, String validateCode) {


        if (StringUtils.isBlank(phone)) {
            buildError(HttpStatus.NOT_FOUND,"手机号不能为空");
        }
        if (StringUtils.isBlank(validateCode)) {
            buildError(HttpStatus.NOT_FOUND,"验证码不能为空");
        }

        Admin adminByPhone = adminMapper.getAdminByPhone(phone);
        if (Objects.isNull(adminByPhone)) {
            buildError(HttpStatus.NOT_FOUND,"该手机号不为管理员！");
        }

        // 验证码通过短信发送且需要通过验证（不区分大小写）
        //(1)先从Redis中获取当前手机号对应的验证码
        String correctValidateCode = redisOperations.get(REDIS_VALIDATE_CODE_KEY_PREFIX + phone);
        //(2)如果Redis中不存在验证码
        if (StringUtils.isBlank(correctValidateCode)) {
            buildError(HttpStatus.NOT_FOUND,"验证码已过期，请重新获取！");
        }
        //(3)如果验证码存在
        else {
            if (!StringUtils.equalsIgnoreCase(correctValidateCode, validateCode)) {
                buildError(HttpStatus.BAD_REQUEST,"验证码填写错误！");
            }
        }
        adminByPhone.setPwd(null);

        return adminByPhone;



    }
}
